package com.codefish.codefishseckill.validator;

import com.codefish.codefishseckill.utils.MobileUtils;
import com.codefish.codefishseckill.validator.annotation.IsMobile;
import org.springframework.util.StringUtils;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * 手机号码校验器
 *
 * @author codefish
 * @version 1.0
 * @date 2022/07/05 下午 04:32
 */
public class IsMobileValidator implements ConstraintValidator<IsMobile, String> {
    private boolean required = false;

    @Override
    public void initialize(IsMobile constraintAnnotation) {
       // ConstraintValidator.super.initialize(constraintAnnotation);
        required = constraintAnnotation.required();
    }

    @Override
    public boolean isValid(String value, ConstraintValidatorContext constraintValidatorContext) {

        if (!required && StringUtils.isEmpty(value)) {
            return true;
        }
        return MobileUtils.isMobileNumber(value);
    }
}
